import requests, json


def getWeather(n):
    """ 
    :return: 无锡天气情况 
    """
    if n == 0:
        url = "https://www.sojson.com/open/api/weather/json.shtml?city=无锡"

        html = requests.get(url).text  # 天气数据 字符串类型
        print("html", html)
        jsonData = json.loads(html)  # 字典类型

        print("jsondata",jsonData)
        data = jsonData['data']['forecast']   # 选取出来今天的数据
        todayWeather = data[0]

        date = todayWeather['date']
        sunrise = todayWeather['sunrise']
        high = todayWeather['high']
        low = todayWeather['low']
        weather = todayWeather['type']
        notice = todayWeather['notice']

        weatherData = {
            'date': date,
            'sunrise': sunrise,
            'high': high,
            'low': low,
            'weather': weather,
            'notice': notice
        }


        with open("weather.json", 'w', encoding='utf-8') as f:
            f.write(json.dumps(weatherData))
    else:
        with open("weather.json", 'r', encoding='utf-8') as f:
            weatherData = json.loads(f.read())

    print("获取到天气", weatherData)
    return weatherData

